package com.lys.config;

import com.lys.interceptors.AdminUserPowerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.List;

/**
 * @author 陆小根
 * @version 9.0.4
 * @Date 2022/5/29 22:57
 * 对静态资源的处理
 */
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/backend/**").addResourceLocations("/backend/");
        registry.addResourceHandler("/common/**").addResourceLocations("/common/");
        registry.addResourceHandler("/files/**").addResourceLocations("/files/");
        registry.addResourceHandler("/pre/**").addResourceLocations("/pre/");
        registry.addResourceHandler("/statics/**").addResourceLocations("/statics/");
        registry.addResourceHandler("/statics/images/**").addResourceLocations("/statics/images/");

    }


    // 配置视图解析器
    @Override
    protected void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setSuffix(".jsp");
        registry.viewResolver(resolver);
    }

    // 文件上传解析器
    @Bean
    public MultipartResolver multipartResolver() {
        CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
        return commonsMultipartResolver;
    }


    // 类型转换器
    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
        MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        converters.add(stringHttpMessageConverter);
        converters.add(mappingJackson2HttpMessageConverter);

    }

    // 配置拦截器
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        AdminUserPowerInterceptor adminUserPowerInterceptor = new AdminUserPowerInterceptor();
        registry.addInterceptor(adminUserPowerInterceptor)
                .addPathPatterns("/admin/product/**")
                .addPathPatterns("/productCategory/**")
                .addPathPatterns("/user/admin/**")
                .addPathPatterns("/admin/order/list");
    }
}
